package medium;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class T17 {
    public static void main(String[] args) {
        System.out.println(letterCombinations("23"));

    }

    public static List<String> letterCombinations(String digits) {
        Map<Integer, char[]> map = new HashMap<>();
        map.put(2, new char[]{'a', 'b', 'c'});
        map.put(3, new char[]{'d', 'e', 'f'});
        map.put(4, new char[]{'g', 'h', 'i'});
        map.put(5, new char[]{'j', 'k', 'l'});
        map.put(6, new char[]{'m', 'n', 'o'});
        map.put(7, new char[]{'p', 'q', 'r', 's'});
        map.put(8, new char[]{'t', 'u', 'v'});
        map.put(9, new char[]{'w', 'x', 'y', 'z'});
        List<String> current = new ArrayList<>();
        List<String> result = new ArrayList<>();
        int length = digits.toCharArray().length;
        if (length == 0) {
            return result;
        }
        for (int i = 0; i < digits.toCharArray().length; i++) {
            f(result, current, map.get(digits.toCharArray()[i] - '0'));
        }
        return result;
    }

    // 合并两个数组
    public static void f(List<String> result, List<String> current, char[] b) {
        if (b.length == 0) {
            return;
        }

        if (current.size() == 0) {
            for (int j = 0; j < b.length; j++) {
                result.add(b[j] + "");
                current.add(b[j] + "");
            }
        } else {
            for (int i = 0; i < current.size(); i++) {
                for (int j = 0; j < b.length; j++) {
                    result.add(current.get(i) + b[j]);
                }
                result.remove(current.get(i));
            }
            current.clear();
            current.addAll(result);
        }
    }
}
